package control;

import com.sun.prism.impl.PrismSettings;
import model.*;
import model.Point;
import view.TextScreen;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;


/**
 * Created with IntelliJ IDEA.
 * User: Peter
 * Date: 11/14/13
 * Time: 7:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class Controller {
    public static final int N_ROWS = 60;
    public static final int N_COLS = 100;
    private JTextArea screen;
    private TextScreen ts;
    private Tank tank = new Tank(new model.Point(47, 35));
    private LinkedHashSet<Alien> aliens;
    private LinkedHashSet<Point> bullets = new LinkedHashSet<Point>();

    public Controller() {
        initScreen();
        //   renderScreen();
        Timer refreshScreen = new Timer(16, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                for (Alien unit : aliens) {
                    ts.drawAlien(unit.getLocation());
                }


                for (Point bullet : bullets) {
                    ts.removeBullet(bullet);
                    bullet.move();
                    ts.drawBullet(bullet.getLocation());
                }


                ts.removeTank(tank.getLocation());
                ts.drawTank(tank.getLocation());

                renderScreen();

            }
        });
        refreshScreen.start();

        Timer alienSpeed = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for (Alien unit : aliens) {
                    if (unit.getLocation().x == 2) {
                        for (Alien i : aliens) {
                            i.setIsLeft(false);
                        }
                    } else if (unit.getLocation().x == 93) {
                        for (Alien i : aliens) {
                            i.setIsLeft(true);
                        }
                    }
                }
                for (MyUnit unit : aliens) {
                    ts.removeAlien(unit.getLocation());
                    unit.move();
                }
            }
        });
        alienSpeed.start();


    }


    public void renderScreen() {
        screen.setText(null);
        screen.append(ts.getRow(N_ROWS - 1));
        for (int i = 0; i < N_ROWS - 1; i++) {
            screen.append(ts.getRow(i) + "\n");
        }
        screen.append(ts.getRow(N_ROWS - 1));
    }

    public void initScreen() {
        JFrame f = new JFrame("TextFrame");
        screen = new JTextArea(N_ROWS, N_COLS);
        screen.setEditable(false);
        screen.setFont(new Font("Courier New", Font.PLAIN, 14));
        screen.setBackground(Color.BLACK);
        screen.setForeground(Color.WHITE);

        f.add(screen);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setResizable(false);
        f.setVisible(true);

        ts = new TextScreen(N_ROWS, N_COLS);
        aliens = new LinkedHashSet<Alien>();
        for (int i = 5; i <= 15; i += 5) {
            for (int j = 15; j < 85; j += 6) {
                aliens.add(new Alien(new model.Point(j, i)));
            }
        }


        screen.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                ts.removeTank(tank.getLocation());
                if (keyCode == e.VK_LEFT) {
                    if (tank.topLeft.x > 1) {
                        tank.topLeft.moveLeft();
//                    setLocation(topLeft);


                    }

                } else if (keyCode == e.VK_RIGHT) {
                    if (tank.topLeft.x < 92) {
                        tank.topLeft.moveRight();

                    }

                }


            }


        });


        screen.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int keyCode = e.getKeyCode();
                if (keyCode == e.VK_SPACE) {
                    bullets.add(new Point(tank.getLocation().x + 3, tank.getLocation().y - 1));

                }

            }
        });




    }


    public static void main(String[] args) {
        Controller app = new Controller();
    }
}

